//
//  OAUTH2.m
//  weiboOC
//
//  Created by 刘耀宗 on 16/4/13.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "OAUTH2.h" 
#import "AFWorkTool.h"
#import "accessAcount.h"
@interface OAUTH2 ()
@property (nonatomic, copy) NSString *url;



@end

@implementation OAUTH2
+(instancetype)shareOauth2
{
    static id instance;
    dispatch_once_t once;
    dispatch_once(&once, ^{
        instance=[[self alloc] init];
    });

    return instance;
}
/*
 #define OauthUrl  @"https://api.weibo.com/oauth2/authorize"
 /// 请应用时分配的AppKey
 #define client_id  @"762518122"
 
 /// 授权回调地址
 #define redirect_uri  @"http://www.baidu.com"*/

-(void)load:(NSString *)code  andSuccess:(void (^)(NSArray *arr))successBlock
{
    self.url=@"https://api.weibo.com/oauth2/access_token";
    //@"client_secret" : client_secret,,@"grant_type": code
//    OauthUrl
    NSMutableDictionary *parameters=@{@"client_id": client_id,
                               @"client_secret" : client_secret,
                               @"grant_type": grant_type,
                               @"code":code,
                               @"redirect_uri":redirect_uri};
    
    [[AFWorkTool shareAFNWorkTool] POST:self.url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        //获取数据成功
        //获取第一个key
//        responseObject.
        NSString *key=responseObject.keyEnumerator.nextObject;
        NSArray *arr=responseObject[key];
        //创建一个可变数组
        NSMutableArray *mutableArr=[NSMutableArray array];
        for (NSDictionary *dic in arr) {
            accessAcount *model=[accessAcount ModelWithDic:dic];
            [mutableArr addObject:model];
        }
        
        NSLog(@"获取数据成功");
        
        successBlock([mutableArr copy]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             NSLog(@"获取数据失败");
    } ];
     
 
     
        
 



}


@end
